﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UserDefinedForms.Entities;
using UserDefinedForms.Business;
using UDFDesign.GUIComponent.CustomControl;
using System.IO;

namespace UDFDesign.GUIComponent.SupForm
{
    public partial class SupplierForm : Form
    {
        #region member
        private DesignForm designForm;
        private SupOrderForm supForm;
        private DesignFormService designFormService = new DesignFormService();
        private SupOrderFormService supFormService = new SupOrderFormService();
        private FormControlService controlService = new FormControlService();
        protected ControlListItemService listItemService = new ControlListItemService();

        private List<DesignableControl> listDeletedControl = new List<DesignableControl>();
        protected List<DesignableControl> listControl = new List<DesignableControl>();

        private Image backGround;
        #endregion

        #region constructor
        public SupplierForm(DesignForm designForm)
        {
            this.designForm = designForm;
            
            if (designForm.FormId > 0)
            {
                supForm = supFormService.GetByFormId(designForm.FormId);
            }
            
            InitializeComponent();
            designPnl.SupForm = this;
            publishToInterface();
        }

        public SupplierForm( int supplierId )
          
        {
            this.designForm = createNewFormEntity();

            this.supForm = createNewSupOrderForm();
            supForm.SupplierId = supplierId;
            InitializeComponent();
            designPnl.SupForm = this;
            publishToInterface();

        }

        public SupplierForm()
        {
            DesignForm f = designFormService.GetByFormId(9);
            this.designForm = f;

            if (designForm.FormId > 0)
            {
                supForm = supFormService.GetByFormId(designForm.FormId);
            }

            InitializeComponent();
            designPnl.SupForm = this;
            publishToInterface();
        }

        #endregion
        
        #region function
        private void testDB()
        {
            /*
            FormControlService fser = new FormControlService();
            FormControl control = fser.GetByControlId(4);
            ControlListItem item = new ControlListItem();
            item.ItemData = "jj";
            control.ControlListItemCollection. Add(item);

            fser.Save(control);
             * */
            listItemService.Delete(4);

        }

        public void addControl(DesignableControl control)
        {
            listControl.Add(control);
        }

        public void deleteControl(DesignableControl control)
        {
            listControl.Remove(control);
            listDeletedControl.Add(control);
        }

        public void save()
        {
            //designFormService.Save(designForm);
            designFormService.Save(designForm);
            TList<FormControl> listControlEntity = new TList<FormControl>();
            foreach (DesignableControl control in listControl)
            {
                FormControl controlEntity = control.ControlMappingObj.toEntity();
                listControlEntity.Add(controlEntity);
                controlEntity.FormId = designForm.FormId;
                TList<ControlListItem> listItem = listItemService.GetByControlId(controlEntity.ControlId);
                listItemService.Delete(listItem);
                listItemService.Insert(controlEntity.ControlListItemCollection);

                
            }
            controlService.Save(listControlEntity);

            TList<FormControl> listDelControlEntity = new TList<FormControl>();
            foreach (DesignableControl delControl in listDeletedControl)
            {
                FormControl controlEntity = delControl.ControlMappingObj.toEntity();
                listDelControlEntity.Add(controlEntity);
                TList<ControlListItem> listItem = listItemService.GetByControlId(controlEntity.ControlId);
                listItemService.Delete(listItem);
            }
            controlService.Delete(listDelControlEntity);

            supForm.FormId = designForm.FormId;
            supFormService.Save(supForm);
        }


        private static DesignForm createNewFormEntity()
        {
            DesignForm form = new DesignForm();
            form.Name = "New Supplier form";
            form.Status = 1;
            form.CreatedDate = DateTime.Now;
            form.ModifiedDate = DateTime.Now;
            form.EffectiveDate = DateTime.Now;
            form.UserId = SystemEnv.getUserId();
            //form.
            return form;
        }

        private static SupOrderForm createNewSupOrderForm()
        {
            SupOrderForm supF = new SupOrderForm();
            return supF;
        }

        private void updateToEntity()
        {
            designForm.Name = nameTxtBox.Text;
            designForm.Description = descriptionTxtBox.Text;
            designForm.EffectiveDate = effectedDatePicker.Value;
            supForm.Background = imageToByteArray(backGround) ;
        }

        public void publishToInterface()
        {
            nameTxtBox.Text = designForm.Name;
            descriptionTxtBox.Text = designForm.Description;
            statusLbl.Text = designForm.Status.ToString();
            createdDateLbl.Text = String.Format("{0:dd/MM/yyyy}", designForm.CreatedDate);
            modifiedDateLbl.Text = String.Format("{0:dd/MM/yyyy}", designForm.ModifiedDate);
            effectedDatePicker.Value = (DateTime)designForm.EffectiveDate;
            userIdLbl.Text = designForm.UserId.ToString();
            setBackground(byteArrayToImage(supForm.Background));

            TList<FormControl> listExistedControl = controlService.GetByFormId(designForm.FormId);
            foreach (FormControl entity in listExistedControl)
            {
                DesignableControl control = ControlFactory.createControl(designPnl, entity);
                designPnl.addControl(control);
            }

        }

        private void setBackground(Image bgr)
        {
            backGround = bgr;
            designPnl.BackgroundImage = bgr;

        }

        private byte[] imageToByteArray(System.Drawing.Image imageIn)
        {
            if (imageIn == null) return new byte[0];
            MemoryStream ms = new MemoryStream();
            imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            return ms.ToArray();
        }

        private Image byteArrayToImage(byte[] byteArrayIn)
        {
            if (byteArrayIn == null|| byteArrayIn.Length==0) return null;
            MemoryStream ms = new MemoryStream(byteArrayIn);
            Image returnImage = Image.FromStream(ms);
            return returnImage;
        }
        #endregion

        #region action

        private void loadBackgroundBtn_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileDlg = new OpenFileDialog();
            fileDlg.Filter = "Image Files|*.jpg;*.gif;*.bmp;*.png;*.jpeg";
            if (fileDlg.ShowDialog() == DialogResult.OK)
            {
                Image bgr = Image.FromFile(fileDlg.FileName);
                setBackground(bgr);
            }
        }


        private void saveBtn_Click(object sender, EventArgs e)
        {
            updateToEntity();
            save();
        }

        #endregion

    }
}
